package com.lg.partition16;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/4/23 10:15
 */
public class P1525NOIP2010提高组_关押罪犯 {

    private static final int N = 20010, M = 200010, INF = 0x3f3f3f3f;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M], color = new int[N];
    private static int n, m, idx = 0;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt();
        m = nextInt();
        Arrays.fill(h, -1);
        int maxw = 0;
        for (int i = 0; i < m; i++) {
            int a = nextInt(), b = nextInt(), c = nextInt();
            add(a, b, c);
            add(b, a, c);
            maxw = Math.max(maxw, c);
        }
        writer.println(resolve(maxw));
        writer.flush();
    }

    private static int resolve(int maxw) {
        //最大值最小，具有二段性，考虑使用二分
        int l = 0, r = maxw;
        while (l < r) {
            int mid = l + r >> 1;
            if (check(mid)) r = mid;
            else l = mid + 1;
        }
        return l;
    }

    private static boolean check(int mid) {
        Arrays.fill(color, 0);
        //两个节点的边权如果大于了mid，说明不能染相同的色
        for (int i = 1; i <= n; i++) {
            if (color[i] == 0 && !dfs(i, 1, mid)) {
                return false;
            }
        }
        return true;
    }

    private static boolean dfs(int u, int col, int mid) {
        color[u] = col;
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (w[i] <= mid) continue;
            if (color[v] == color[u] || (color[v] == 0 && !dfs(v, 3 - col, mid))) {
                return false;
            }
        }
        return true;
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
